<?php
/**
 * 车辆管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/19
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\CarBrandModel;
use app\common\model\CarLabelModel;
use app\common\model\CarModel;
use app\common\model\CarSeriesModel;
use app\common\model\MemberModel;
use think\facade\Config;
use think\facade\Request;

class CarController extends AdminBaseController
{
    /**
     * 推荐列表
     */
    public function index(CarModel $car){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$car->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    public function detail($id){
        $model = Request::param("model");

        if(empty($model) || $model==""){
            $this->error("参数错误！");
        }

        if($model=='brand'){
            $m=new CarBrandModel();
        }else if($model=='series'){
            $m=new CarSeriesModel();
        }else if($model=='car'){
            $m=new CarModel();
        }

        $field='id,status,brand_id,series_id,series_factory_id,create_time,update_time';

        if($model=='brand'){
            $title=Config::get('tableName.car_brand');
        }else if($model=='series'){
            $title=Config::get('tableName.car_series');
        }else if($model=='car'){
            $field.=',brand_logo';
            $title=Config::get('tableName.car');
        }
        $info=$m::field($field,true)->get($id);

        $this->assign("info",$info->toArray());
        $this->assign("id",$id);
        $this->assign("title",$title);
        $this->assign("model",$model);
        return $this->fetch();
    }

    /**
     * 汽车品牌
     * @param CarBrandModel $brand
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function brand(CarBrandModel $brand){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$brand->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 品牌状态
     */
    public function brand_status(){
        $id = Request::post("id");
        $status = Request::post("status");
        $model = Request::post("model");

        if(empty($model)){
            $am = CarBrandModel::get($id);
        }else if($model=="conf"){
            $am = CarLabelModel::get($id);
        }

        $am->status = $status;
        $st = $am->save();

        if ($st) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }

    /**
     * 品牌首页菜单推荐
     */
    public function brand_hot(){
        $id = Request::post("id");
        $hot = Request::post("hot");
        $type = Request::post("type");

        $am = CarBrandModel::get($id);
        if(empty($type)){
            $am->hot = $hot;
        }else{
            $am->shot = $hot;
        }

        $st = $am->save();

        if ($st) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }

    /**
     * 删除品牌
     * @param MessageModel $m
     */
    public function brand_del(CarBrandModel $m)
    {
        $id = Request::post("id");
        $type = Request::post("type");

        if(empty($type)){
            $status = $m->del($id);
            if ($status['status']) {
                $this->success($status['msg']);
            } else {
                $this->error($status['msg']);
            }

        }else{
            $error = 0;
            $sussuc = 0;
            foreach ($id as $vo) {
                $status = $m->del($vo);
                if (!$status['status']) {
                    $error++;
                    continue;
                } else {
                    $sussuc++;
                }
            }

            $this->success("成功删除{$sussuc}条，删除失败{$error}条");
        }

    }

    /**
     * 删除品牌
     * @param MessageModel $m
     */
    public function del(CarBrandModel $m)
    {
        $id = Request::post("id");
        $type = Request::post("type");
        $model = Request::post("model");

        if(empty($model) || $model==""){
            $this->error("参数错误！");
        }

        if($model=='brand'){
            $m=new CarBrandModel();
        }else if($model=='series'){
            $m=new CarSeriesModel();
        }else if($model=='car'){
            $m=new CarModel();
        }else if($model=='conf'){
            $m=new CarLabelModel();
        }

        if(empty($type)){
            $status = $m->del($id);
            if ($status['status']) {
                $this->success($status['msg']);
            } else {
                $this->error($status['msg']);
            }

        }else{
            $error = 0;
            $sussuc = 0;
            foreach ($id as $vo) {
                $status = $m->del($vo);
                if (!$status['status']) {
                    $error++;
                    continue;
                } else {
                    $sussuc++;
                }
            }

            $this->success("成功删除{$sussuc}条，删除失败{$error}条");
        }

    }


    /**
     * 搜索
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function search($data)
    {
        $where = [];
        if (!empty($data["mobile"])) {
            $u = MemberModel::where('mobile', $data["mobile"])
                ->field('uid')
                ->find();

            $where[] = ["user_id", '=', $u['uid']];
        }

        if (!empty($data["brand_name"])) {
            $where[] = ["brand", 'like', "%{$data['brand_name']}%"];
        }

        if (!empty($data["title"])) {
            $where[] = ["title", 'like', "%{$data['title']}%"];
        }

        if (!empty($data["series_name"])) {
            $where[] = ["series_name", 'like', "%{$data['series_name']}%"];
        }

        if (!empty($data["car_name"])) {
            $where[] = ["car_name", 'like', "%{$data['car_name']}%"];
        }

        if (!empty($data["status"]) && $data["status"] != -2) {
            $where[] = ["status", '=', $data['status'] - 1];
        }

        return $where;
    }

    /**
     * 车系列表
     * @param CarSeriesModel $cs
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function series(CarSeriesModel $cs){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$cs->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }
    /**
     * 品牌状态
     */
    public function series_status(){
        $id = Request::post("id");
        $status = Request::post("status");

        $am = CarSeriesModel::get($id);
        $am->status = $status;
        $st = $am->save();

        if ($st) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }

    /**
     * 删除品牌
     * @param MessageModel $m
     */
    public function series_del(CarSeriesModel $m)
    {
        $id = Request::post("id");
        $type = Request::post("type");

        if(empty($type)){
            $status = $m->del($id);
            if ($status['status']) {
                $this->success($status['msg']);
            } else {
                $this->error($status['msg']);
            }

        }else{
            $error = 0;
            $sussuc = 0;
            foreach ($id as $vo) {
                $status = $m->del($vo);
                if (!$status['status']) {
                    $error++;
                    continue;
                } else {
                    $sussuc++;
                }
            }

            $this->success("成功删除{$sussuc}条，删除失败{$error}条");
        }

    }

    /**
     * 编辑
     * @param int $id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit($id=0){
        $model = Request::param("model");

        if(empty($model) || $model==""){
            $this->error("参数错误！");
        }

        if($model=='brand'){
            $m=new CarBrandModel();
        }else if($model=='series'){
            $m=new CarSeriesModel();
        }else if($model=='car'){
            $m=new CarModel();
        }else if($model=='conf'){
            $m=new CarLabelModel();
        }

        if(Request::isPost()){
            $data=Request::param();

            $status=$m->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $field='id,status,brand_id,series_id,series_factory_id,create_time,update_time';

            if($model=='brand'){
                $title=Config::get('tableName.car_brand');
            }else if($model=='series'){
                $title=Config::get('tableName.car_series');
            }else if($model=='car'){
                $field.=',brand_logo';
                $title=Config::get('tableName.car');
            }else if($model=='conf'){
                $field="create_time,list_order,id,status";
                $title=Config::get('tableName.conf');
            }

            if(!$id){
                $info=[
                    "title"=>"",
                    "thumb"=>"",
                ];
            }else{
                $info=$m::field($field,true)->get($id);
                $info=$info->toArray();
            }

            $this->assign("info",$info);
            $this->assign("id",$id);
            $this->assign("title",$title);
            $this->assign("model",$model);
            return $this->fetch();
        }
    }

    /**
     * 加装配置管理
     * @param CarLabelModel $car
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function carConfig(CarLabelModel $car){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$car->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 排序
     */
    public function acOrder(){
        $id=Request::post("id");
        $orders=Request::post("orders");

        $am=CarLabelModel::get($id);
        $am->list_order=$orders;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }
}